package boardgamer.yavalath.client.mainmenu;

import android.graphics.Canvas;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.MotionEvent;
import boardgamer.common.client.ClickableButton;
import boardgamer.common.client.Drawable;
import boardgamer.common.client.HasEventHandler;
import boardgamer.common.client.SimpleButton;
import boardgamer.common.client.ToggleButton;
import boardgamer.common.client.layout.BitmapLayout;
import boardgamer.common.client.layout.HasLayout;
import boardgamer.common.client.layout.InvisibleRect;
import boardgamer.common.client.layout.LayoutGroup;
import boardgamer.yavalath.client.GlowHex;
import boardgamer.yavalath.client.LetterHex;
import boardgamer.yavalath.client.Navigator;
import boardgamer.yavalath.client.RenderHelper;
import boardgamer.yavalath.client.Settings;
import boardgamer.yavalath.client.resources.ImageHandler;
import boardgamer.yavalath.client.resources.SoundHandler;

/**
 * Panel containing menu buttons to be displayed in the main menu
 *
 * @author garysoed
 */
public class MenuPanel implements Drawable, HasLayout, HasEventHandler {

  private static final float MENU_PANEL_HEIGHT_RATIO = 0.2f;

  private final ClickableButton ruleButton;
  private final ToggleButton muteButton;
  private final LayoutGroup group;

  public MenuPanel(int color, Rect canvasRect) {
    this.ruleButton = createRuleButton(color);
    this.muteButton = createMuteButton(color);

    this.group = new LayoutGroup();
    this.group.add(muteButton).atPosition(0, 0);
    this.group
        .add(ruleButton).atPosition(RenderHelper.instance().getVHexUpRightDisplacement());
    // Offset the bounding rect of this group to 0, 0, since the alignment algorithm assumes that
    // this group has bounding rect with left and top set to 0
    RectF groupRect = this.group.getBoundingRect();
    groupRect.offsetTo(0, 0);
    this.group.setBoundingRect(groupRect);

    LayoutGroup outerGroup = new LayoutGroup();
    outerGroup.add(this.group).atCurrentRect();
    outerGroup.setBoundingRect(new RectF(
        0,
        0,
        canvasRect.width(),
        canvasRect.height() * MENU_PANEL_HEIGHT_RATIO));
    outerGroup.add(new InvisibleRect(canvasRect)).atCurrentRect()
        .horizontalAlign(LayoutGroup.HorizontalAlign.RIGHT)
        .verticalAlign(LayoutGroup.VerticalAlign.BOTTOM);
  }

  private ClickableButton createRuleButton(int color) {
    LetterHex display = new LetterHex('?', color, new PointF(),
        RenderHelper.instance().getHexLetterPaint(), true);

    RectF collisionRect = display.getBoundingRect();
    collisionRect.inset(5, 5);

    return new ClickableButton(
        new SimpleButton(
            display,
            new HasEventHandler.EventHandler() {
              @Override
              public void onEvent(float eventX, float eventY, int eventAction) {
                if (eventAction == MotionEvent.ACTION_UP) {
                  Navigator.instance().showRule();
                  SoundHandler.playWave(SoundHandler.buttonSound);
                }
              }
            },
            collisionRect),
        GlowHex.verticalHex(display, color));
  }

  private ToggleButton createMuteButton(int color) {
    HasEventHandler.EventHandler soundToggleHandler
        = new HasEventHandler.EventHandler() {
      @Override
      public void onEvent(float eventX, float eventY, int eventAction) {
        if (eventAction == MotionEvent.ACTION_UP) {
          SoundHandler.playWave(SoundHandler.buttonSound);
          Settings.instance().setMuted(MenuPanel.this.muteButton.isActive());
        }
      }
    };

    Paint bitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    bitmapPaint.setColorFilter(new LightingColorFilter(color, 0));
    BitmapLayout soundOnDisplay = new BitmapLayout(ImageHandler.soundOn, bitmapPaint);
    BitmapLayout soundOffDisplay = new BitmapLayout(ImageHandler.soundOff, bitmapPaint);
    RectF collisionRect = soundOnDisplay.getBoundingRect();
    collisionRect.inset(5, 5);

    ClickableButton soundOnButton = new ClickableButton(
        new SimpleButton(soundOnDisplay, soundToggleHandler, collisionRect),
        GlowHex.verticalHex(soundOnDisplay, color));
    ClickableButton soundOffButton = new ClickableButton(
        new SimpleButton(soundOffDisplay, soundToggleHandler, collisionRect),
        GlowHex.verticalHex(soundOffDisplay, color));

    return new ToggleButton(soundOnButton, soundOffButton, !Settings.instance().isMuted());
  }

  @Override
  public void draw(Canvas canvas) {
    ruleButton.draw(canvas);
    muteButton.draw(canvas);
  }

  @Override
  public void handleEvent(float eventX, float eventY, int eventAction) {
    ruleButton.handleEvent(eventX, eventY, eventAction);
    muteButton.handleEvent(eventX, eventY, eventAction);
  }

  @Override
  public RectF getCollisionBox() {
    return getBoundingRect();
  }

  @Override
  public void setBoundingRect(RectF boundingRect) {
    group.setBoundingRect(boundingRect);
  }

  @Override
  public RectF getBoundingRect() {
    return group.getBoundingRect();
  }
}
